import 'package:flutter/material.dart';

class LoginPage extends StatefulWidget {
  const LoginPage({super.key});

  @override
  State<LoginPage> createState() => _LoginPageState();
}

class _LoginPageState extends State<LoginPage> {
  var showPossword = false;
  // 添加控制器来获取输入框的值
  final TextEditingController _usernameController = TextEditingController();
  final TextEditingController _passwordController = TextEditingController();
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('登陆'),
        centerTitle: true,
        backgroundColor: Colors.green,
      ),
      body: SafeArea(
        minimum: EdgeInsets.all(20),
        child: ListView(
          children: [
            TextField(
              controller: _usernameController,
              decoration: InputDecoration(
                labelText: '用户名',
                hintText: '请输入用户名',
                // 未获取到焦点时，下划线设置为灰色
                enabledBorder: UnderlineInputBorder(
                  borderSide: BorderSide(color: Colors.grey),
                ),
                // 获取到焦点时，下划线设置为蓝色
                focusedBorder: UnderlineInputBorder(
                  borderSide: BorderSide(color: Colors.blue),
                ),
                prefixIcon: Icon(Icons.person),
              ),
            ),
            Padding(padding: EdgeInsets.all(5)),
            TextField(
              controller: _passwordController,
              decoration: InputDecoration(
                labelText: '密码',
                hintText: '请输入密码',
                // 未获取到焦点时，下划线设置为灰色
                enabledBorder: UnderlineInputBorder(
                  borderSide: BorderSide(color: Colors.grey),
                ),
                // 获取到焦点时，下划线设置为蓝色
                focusedBorder: UnderlineInputBorder(
                  borderSide: BorderSide(color: Colors.blue),
                ),
                prefixIcon: Icon(Icons.lock),
                suffixIcon: IconButton(
                  onPressed: () {
                    setState(() {
                      showPossword = !showPossword;
                    });
                  },
                  icon: Icon(
                    showPossword ? Icons.visibility_off : Icons.visibility,
                  ),
                ),
                hintMaxLines: 1,
              ),
              obscureText: !showPossword,
            ),
            Padding(padding: EdgeInsets.all(10)),
            ElevatedButton(
              onPressed: () {
                // 处理登录逻辑
                String username = _usernameController.text;
                String password = _passwordController.text;
                print('用户名: $username, 密码: $password');
                Navigator.pushReplacementNamed(context, '/');
              },
              style: ButtonStyle(
                backgroundColor: WidgetStateProperty.all(Color(0xFF4CAF50)),
              ),
              child: Text('登录'),
            ),
            Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                Text('没有账号，'),
                TextButton(
                  onPressed: () {
                    Navigator.pushReplacementNamed(context, '/register');
                  },
                  child: Text('去注册', style: TextStyle(color: Colors.green)),
                ),
              ],
            ),
          ],
        ),
      ),
    );
  }
}
